package com.ys.controller.web.storage;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.entity.storage.TccLocation;
import com.ys.service.sys.CommonApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web/storage/tccLocation")
public class WebTccLocationController {

	@Autowired
	private CommonApiService<TccLocation> commonApiService;

	Long menuId = MenuId.TCC_Location;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		try {
			TccLocation entity = new TccLocation();
			entity.setChw(commonApiService.getCommonNextUkey(MenuId.TCC_Location, "chw"));
			entity.setStatus(1);
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("feeStandard", "0.03");
			return commonApiService.toAdd(model, entity, menuId, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
						 Long id){
		if (id == null){
			return W.error(model, "id不能为空");
		}

		if (new TccLocation().setId(id).count() == 0){
			return W.error(model, "该信息不存在!");
		}

		try {
			Map<String, Object> extraParam = getExtraParam(id);
			return commonApiService.toEdit(model, id, new TccLocation(), MenuId.TCC_Location, extraParam, null);
		} catch (Exception e) {

			e.printStackTrace();
			return W.error(model, e.getMessage());
		}


	}
	public Map<String,Object> getExtraParam(Long id){
		Map<String,Object> extraParam = new TccLocation()
				.addLeftJoin("TCC_WarehouseArea B ON B.Ckq=A.Ckq")
				.addLeftJoin("TCC_Warehouse C ON C.Cck=B.Cck")
				.addLeftJoin("TB_Country tb ON tb.code = C.countryCode")
				.addField("C.Cck cck,C.Vck vck,C.CountryCode countryCode,C.City city,tb.name countryName,B.Ckq ckq,B.Vkq vkq")
				.setId(id)
				.queryFirstMap();

		return extraParam;
	}

}
